# -*- coding: utf-8 -*-
# @Time    : 2020/6/20 上午12:44
# @Author  : caotian
# @FileName: convolution3.py
# @Software: PyCharm
import matplotlib.pyplot as plt
import paddle.fluid as fluid
from paddle.fluid.dygraph.nn import Conv2D
import numpy as np
from paddle.fluid.initializer import NumpyArrayInitializer
from PIL import Image

img=Image.open('./images/section1/000000355610.jpg').convert('L')
img=np.array(img)

with fluid.dygraph.guard():
    w=np.ones([1,1,5,5],dtype='float32')/25
    conv=Conv2D(num_channels=1,num_filters=1,filter_size=[5,5],param_attr=fluid.ParamAttr(initializer=NumpyArrayInitializer(value=w)))
    x=img.astype('float32')
    x=x.reshape(1,1,img.shape[0],img.shape[1])
    x=fluid.dygraph.to_variable(x)
    y=conv(x)
    out=y.numpy()

plt.figure(figsize=(20,12))
f=plt.subplot(121)
f.set_title('input image')
plt.imshow(img,cmap='gray')
f=plt.subplot(122)
f.set_title('output feature map')
out = out.squeeze()
plt.imshow(out,cmap='gray')
plt.show()
